package com.example.demo.test.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.EmptyUtil;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping(value = "/wx")
public class WxController {
	
	private static String token = "1510636326801117184";
	
	private String appid = "wx1b2e3c198b317a9d";
	
	private String appsecret = "a38f521c468a6bd455b9fe32ab5937f0";
	
	@RequestMapping(value = "/checksignature")
	public String checkSignature(HttpServletRequest request) {
		String signature = request.getParameter("signature");
		
		String timestamp = request.getParameter("timestamp");
		
		String nonce = request.getParameter("nonce");
		
		String echostr = request.getParameter("echostr");
		
		log.info("signature>>>{}",signature);
		log.info("timestamp>>>{}",timestamp);
		log.info("nonce>>>{}",nonce);
		log.info("echostr>>>{}",echostr);
		
		List<String> tmpList = new ArrayList<>();
		
		tmpList.add(token);
		tmpList.add(timestamp);
		tmpList.add(nonce);
		
		Collections.sort(tmpList, (o1,o2)->{
			try {
				String s1 = new String(o1.getBytes("GB2312"),"ISO-8859-1");
				String s2 = new String(o2.getBytes("GB2312"),"ISO-8859-1");
				return s1.compareTo(s2);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return 0;
		});
		
		log.info("templist>>>{}",JSONObject.toJSONString(tmpList));
		
		String tmpStr = tmpList.stream().map((strNode) -> {return strNode;}).collect(Collectors.joining());
		
		tmpStr = SecureUtil.sha1(tmpStr);
		
		log.info("sha1TmpStr>>>{}",tmpStr);
		
		if(tmpStr.equals(signature)) {
			return echostr;
		}
		
		return null;
	}
	
	@RequestMapping(value = "/getwxuserinfo")
	public Object getWxUserInfo(String code,String state) {
		
		log.info("code>>>{},state>>>{}",code,state);
		
		StringBuilder oauth2Url = new StringBuilder()
				.append("https://api.weixin.qq.com/sns/oauth2/access_token?appid=")
				.append(appid)
				.append("&secret=")
				.append(appsecret)
				.append("&code=")
				.append(code)
				.append("&grant_type=authorization_code");
		
		String retStr = HttpUtil.createGet(oauth2Url.toString()).setConnectionTimeout(3000)
			.execute().body();
		
		log.info("oauthor2接口返回的数据>>>{}",retStr);
		
		if(EmptyUtil.isEmpty(retStr)) {
			return "oauthor2接口认证失败";
		}
		
		JSONObject oauth2Info = JSONObject.parseObject(retStr);
		String accessToken = oauth2Info.getString("access_token");
		String openId = oauth2Info.getString("openid");
		
		log.info("accessToken>>>{},openId>>>{}",accessToken,openId);
		
		StringBuilder userApi = new StringBuilder()
				.append("https://api.weixin.qq.com/sns/userinfo?access_token=")
				.append(accessToken)
				.append("&openid=")
				.append(openId)
				.append("&lang=zh_CN");
		
		String userStr = HttpUtil.createGet(userApi.toString()).setConnectionTimeout(3000).execute().body();
		
		log.info("userInfo>>>{}",userStr);
		
		return userStr;
	}
	
}
